﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class cms_Admin_Danhgia_u_Danhgiacongnhan_QuanDoc : System.Web.UI.UserControl
{
    #region[Khai bao]
    string uID = "";
    static Double MaxPoint = 0;
    static Double Diemchia = 0;
    static Double Total = 0;
    static Double PointNV = 0;
    static Double PointLD = 0;
    #endregion
    #region[Page_Load]
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] != null) { uID = Session["uid"].ToString(); }
        List<tbConfigDATA> list = tbConfigDB.tbConfig_GetAll();
        if (list.Count > 0)
        {
            MaxPoint = Convert.ToDouble(list[0].coMaxPoint);
            Total = Convert.ToDouble(list[0].coTotal);
            PointNV = Convert.ToDouble(list[0].coPointNV);
            PointLD = Convert.ToDouble(list[0].coPointLD);
        }
        list.Clear();
        list = null;
        if (!IsPostBack)
        {
            Viewsub();
        }
    }
    #endregion
    #region[Viewsub]
    void Viewsub()
    {
        //drlNhanvien.Items.Clear();
        //drlNhanvien.Items.Add(new ListItem("===Chọn công nhân===", "0"));
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetBySub_CN_QuanDoc(uID);
        //if (list.Count > 0)
        //{
        //    for (int i = 0; i < list.Count; i++)
        //    {
        //        drlNhanvien.Items.Add(new ListItem(list[i].uName, list[i].uID));
        //    }
        //}
        rptCongnhan.DataSource = list;
        rptCongnhan.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptCongnhan.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Danh gia]
    protected void lbtCapnhat_Click(object sender, EventArgs e)
    {
        if (Session["uIDu"] != null)
        {
            List<tbAssessmentDATA> list = tbAssessmentDB.tbAssessment_GetByCheckuID(Session["uIDu"].ToString());
            if (list.Count > 0)
            {
                list[0].assId = list[0].assId;
                list[0].uID = list[0].uID;
                list[0].uIDLD = uID;
                tbAssessmentDB.tbAssessment_Update_UserID(list[0]);
                List<tbAssessmentDetailDATA> listde = tbAssessmentDetailDB.tbAssessmentDetail_GetByassId(list[0].assId);
                if (listde.Count > 0)
                {
                    DropDownList drlDiem;
                    Label lblID;
                    for (int i = 0; i < rptDanhsach.Items.Count; i++)
                    {
                        drlDiem = (DropDownList)rptDanhsach.Items[i].FindControl("drlDiem");
                        lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
                        for (int j = 0; j < listde.Count; j++)
                        {
                            if (lblID.Text == listde[j].criId)
                            {
                                List<tbAssessmentDetailDATA> listID = tbAssessmentDetailDB.tbAssessmentDetail_GetByID(listde[j].adeId);
                                if (listID.Count > 0)
                                {
                                    listID[0].adeId = listde[j].adeId;
                                    listID[0].adePointLD = drlDiem.SelectedValue;
                                    listID[0].adePointPercentLD = Convert.ToString((Convert.ToDouble(drlDiem.SelectedValue) * Diemchia * PointLD) / Total);
                                    if (tbAssessmentDetailDB.tbAssessmentDetail_Update_PointLD(listID[0]))
                                    { lblLoi.Text = "Đánh giá nhân viên thành công !!"; }
                                }
                                listID.Clear();
                                listID = null;
                            }
                        }
                    }
                }
                listde.Clear();
                listde = null;
            }
            else
            {
                int month = DateTime.Now.Month - 1;
                string assDateBegin = month.ToString() + "/" + "01" + "/" + DateTime.Now.Year.ToString();
                DateTime assDateEnd = vmmsclass.vmmsdate.GetLastDayOfMonth(Convert.ToDateTime(assDateBegin.ToString()));
                if (rptDanhsach.Items.Count > 0)
                {
                    tbAssessmentDATA ObjtbAssessmentDATA = new tbAssessmentDATA();
                    ObjtbAssessmentDATA.assDateBegin = assDateBegin;
                    ObjtbAssessmentDATA.assDateEnd = assDateEnd.ToString();
                    ObjtbAssessmentDATA.assPoint = "0";
                    ObjtbAssessmentDATA.assPointLD = "0";
                    ObjtbAssessmentDATA.assTotalpoint = "0";
                    ObjtbAssessmentDATA.uID = Session["uIDu"].ToString();
                    ObjtbAssessmentDATA.uIDLD = uID;
                    try
                    {
                        if (tbAssessmentDB.tbAssessment_Add(ObjtbAssessmentDATA))
                        {
                            List<tbAssessmentDATA> listmax = tbAssessmentDB.tbAssessment_GetByMax();
                            DropDownList drlDiem;
                            Label lblID;
                            for (int i = 0; i < rptDanhsach.Items.Count; i++)
                            {
                                drlDiem = (DropDownList)rptDanhsach.Items[i].FindControl("drlDiem");
                                lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
                                tbAssessmentDetailDATA ObjtbAssessmentDetailDATA = new tbAssessmentDetailDATA();
                                ObjtbAssessmentDetailDATA.assId = listmax[0].assId;
                                ObjtbAssessmentDetailDATA.criId = lblID.Text;
                                ObjtbAssessmentDetailDATA.adePoint = "0";
                                ObjtbAssessmentDetailDATA.adePointPercent = "0";
                                ObjtbAssessmentDetailDATA.adePointLD = drlDiem.SelectedValue;
                                ObjtbAssessmentDetailDATA.adePointPercentLD = Convert.ToString((Convert.ToDouble(drlDiem.SelectedValue) * Diemchia * PointLD) / Total);
                                ObjtbAssessmentDetailDATA.adeTotalpointPercent = Convert.ToString((Convert.ToDouble(drlDiem.SelectedValue) * Diemchia * PointLD) / Total);
                                ObjtbAssessmentDetailDATA.adeType = "1";
                                if (tbAssessmentDetailDB.tbAssessmentDetail_Add(ObjtbAssessmentDetailDATA))
                                {
                                    lblLoi.Text = "Đánh giá nhân viên thành công !!";
                                }
                            }
                            listmax.Clear();
                            listmax = null;
                        }
                    }
                    catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
                }
            }
            list.Clear();
            list = null;
        }
        paDanhgia.Visible = false;
        paDanhsach.Visible = true;
        Viewsub();
        rptDanhsach.DataSource = null;
        rptDanhsach.DataBind();
    }
    #endregion
    #region[drlNhanvien_change]
    protected void drlNhanvien_SelectedIndexChanged(object sender, EventArgs e)
    {
        Viewtieuchi();
    }
    #endregion
    #region[Viewtieuchi]
    void Viewtieuchi()
    {
        if (drlNhanvien.SelectedValue != "0")
        {
            List<tbAssessmentDATA> list1 = tbAssessmentDB.tbAssessment_GetByuID_DaDanhGiaCaNhan(drlNhanvien.SelectedValue);
            if (list1.Count > 0)
            {
                List<tbUsersDATA> listus = tbUserDB.tbUsers_GetByID(drlNhanvien.SelectedValue);
                string ps = listus[0].psID;
                listus.Clear();
                listus = null;
                List<tbCriteriaDATA> list = tbCriteriaDB.tbCriteria_GetBypsID(ps);
                rptDanhsach.DataSource = list;
                rptDanhsach.DataBind();
                for (int i = 0; i < list.Count; i++)
                {
                    DropDownList drlDiem = (DropDownList)rptDanhsach.Items[i].FindControl("drlDiem");
                    Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
                    int k = (i + 1);
                    lblorder.Text = k.ToString();
                    drlDiem.Items.Clear();
                    for (int j = 1; j < MaxPoint + 1; j++)
                    {
                        drlDiem.Items.Add(j.ToString());
                    }
                }
                MaxPoint = MaxPoint * list.Count;
                Diemchia = Total / MaxPoint;
                list.Clear();
                list = null;
            }
            else
            {
                lblThongbao.Text = "Nhân viên này chưa có điểm tự đánh giá !!";
                return;
            }
            list1.Clear();
            list1 = null;
        }
        else
        {
            rptDanhsach.DataSource = null;
            rptDanhsach.DataBind();
        }
    }
    #endregion
    #region[ViewPontuId]
    protected string ViewPointuID(string suID)
    {
        string Chuoi = "";
        List<tbAssessmentDATA> list = tbAssessmentDB.tbAssessment_GetByuID(suID);
        if (list.Count > 0)
        {
            Chuoi = common.RoundNum(list[0].assPoint.ToString());
        }
        list.Clear();
        list = null;
        return Chuoi;
    }
    protected string ViewPointuIDLD(string suID)
    {
        string Chuoi = "";
        List<tbAssessmentDATA> list = tbAssessmentDB.tbAssessment_GetByuID(suID);
        if (list.Count > 0)
        {
            Chuoi = common.RoundNum(list[0].assPointLD.ToString());
        }
        list.Clear();
        list = null;
        return Chuoi;
    }
    #endregion
    #region[rptCongnhan_ItemCommand]
    protected void rptCongnhan_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string idu = "";
        string ten = e.CommandName.ToString();
        idu = e.CommandArgument.ToString();
        Session["uIDu"] = idu;
        switch (ten)
        {
            case "View":
                List<tbUsersDATA> listus = tbUserDB.tbUsers_GetByID(idu);
                string ps = listus[0].psID;
                listus.Clear();
                listus = null;
                rptDanhsach.DataSource = null;
                rptDanhsach.DataBind();
                List<tbCriteriaDATA> list = tbCriteriaDB.tbCriteria_GetBypsID(ps);
                rptDanhsach.DataSource = list;
                rptDanhsach.DataBind();
                for (int i = 0; i < list.Count; i++)
                {
                    DropDownList drlDiem = (DropDownList)rptDanhsach.Items[i].FindControl("drlDiem");
                    Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
                    Label lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
                    int k = (i + 1);
                    lblorder.Text = k.ToString();
                    drlDiem.Items.Clear();
                    List<tbAssessmentDetailDATA> listac = tbAssessmentDetailDB.tbAssessmentDetail_GetByuID(idu, lblID.Text);
                    for (int j = 1; j < MaxPoint + 1; j++)
                    {
                        drlDiem.Items.Add(j.ToString());
                    }
                    if (listac.Count > 0)
                    {
                        drlDiem.SelectedValue = listac[0].adePointLD;
                    }
                    listac.Clear();
                    listac = null;
                }
                MaxPoint = MaxPoint * list.Count;
                Diemchia = Total / MaxPoint;
                list.Clear();
                list = null;
                paDanhgia.Visible = true;
                paDanhsach.Visible = false;
                break;
        }
    }
    #endregion
    #region[Trolai]
    protected void lbtTrolai_Click(object sender, EventArgs e)
    {
        paDanhgia.Visible = false;
        paDanhsach.Visible = true;
        Viewsub();
        rptDanhsach.DataSource = null;
        rptDanhsach.DataBind();
    }
    #endregion
}
